<?php
//Make this file contain all the functions for retrieving from the database.

function drawreport()
{
	$db = mysql_connect("mysql513.in.shared-server.net:16033", "JRtu434", "qvoiceDB1");
 	mysql_select_db("JRtu434", $db);
 	mysql_query("set SQL_BIG_Selects=1");
	$result = mysql_query("Select projects.rid as RID, name as name,
	cpc*ss as mmkingaku,
	cpc*sum(if(status=\"complete\",1,0)) as total
	from testredirect,projects
	where testredirect.rid=projects.rid 
    group by name order by total desc");
	echo "<table border='1'>\n";
	echo "<tr><th>" . "RID". "</th>\n";
	echo "<th>" . "Name". "</th>\n";
	echo "<th>" . "Contracted". "</th>\n";
	echo "<th>" . "Total". "</th>\n";
	echo "</tr>";
	while($row = mysql_fetch_array($result))
	  {
	  	echo "<tr><td>" . $row['RID'] . "</td>\n";
		echo "<td>" . $row['name'] . "</td>\n";
		echo "<td>" .money_format('%n',$row['mmkingaku']) . "</td>\n";
		echo "<td>" .money_format('%n',$row['total']) . "</td>\n";
		echo "</tr>\n";
	  }
	echo "</table>\n";  
	mysql_close();
}
function drawmitsumoridropbox($id)
{
	$db = mysql_connect("mysql513.in.shared-server.net:16033", "JRtu434", "qvoiceDB1");
 	mysql_select_db("JRtu434", $db);
	mysql_query("Set names utf8");
	$result = mysql_query("select ID,subject from RFQS order by odate desc");
	?><select name="mitsumori" id="mitsumori-picker" class="idbrowser" style="width:200px;">
	<?php
	while ($record = mysql_fetch_array($result))
	{
		if($id == $record[0]) {
	?><option value="<?=$record[0]?>" selected="selected" style="width:auto;"><?=$record[1]?></option><?php	
		}
		else {
	?><option value="<?=$record[0]?>" style="width:auto;"><?=$record[1]?></option><?php		
		}
	
	}
	echo "</select>";
}
function exportmailinglist()
{

	header('Content-type: text/csv');
	header('Content-Disposition: attachment; filename="monitorIDs.csv');
	$fname = 'php://output';
	$fp = fopen($fname,'w');
	$db = mysql_connect("mysql513.in.shared-server.net:16033", "JRtu434", "qvoiceDB1");
	mysql_select_db("JRtu434", $db);
	$result = mysql_query("select companies.NAME as cName ,contacts.NAME,contacts.EMAIL,companies.STATUS from contacts,companies where contacts.COMPID=companies.ID and companies.STATUS in (\"client\",\"pot\") order by cNAME asc");
	fputcsv($fp, array("CompanyName","FirstName","SecondName","Email"));
	while($record = mysql_fetch_array($result))
	{
		$exploded = explode(" ",$record[1]);
		fputcsv($fp, array($record[0],$exploded[0],$exploded[1],$record[2]));
	}
	mysql_close();
	fclose($fp);
	exit;
}


function exportcsv($item)
{

	header('Content-type: text/csv');
	header('Content-Disposition: attachment; filename="monitorIDs.csv');
	$fname = 'php://output';
	$fp = fopen($fname,'w');
	$db = mysql_connect("mysql513.in.shared-server.net:16033", "JRtu434", "qvoiceDB1");
	mysql_select_db("JRtu434", $db);
	$id = mysql_query("Select rid from projects where name=\"".$item."\"");
	$row = mysql_fetch_array($id);
	$sql_str = "";
	if(mysql_num_rows($id)>1)
	{
		$sql_str = " in (\"".$row[0]."\",";
		$i = 1;
		while($i < mysql_num_rows($id)-1)
		{
			$row = mysql_fetch_array($id);
			$sql_str .= "\"".$row[0]."\",";
			$i++;
		}
		$row = mysql_fetch_array($id);
		$sql_str .= "\"".$row[0]."\")";
	}
	else
	 $sql_str = "=\"".$row[0]."\"";

	$mids = mysql_query("Select ID,monID,status from testredirect where rid".$sql_str);
	$list = array();
	fputcsv($fp, array("ID","mid","status"));
	while ($row2 = mysql_fetch_array($mids))
	{	
		fputcsv($fp, array($row2[0],$row2[1],$row2[2]));	
	}
	mysql_close();
	fclose($fp);
	exit;
}
function ghostproject($id)
{
	if($id>0)
	{
	$db = mysql_connect("mysql513.in.shared-server.net:16033", "JRtu434", "qvoiceDB1");
	mysql_select_db("JRtu434", $db);
	mysql_query("set SQL_BIG_Selects=1");
	mysql_query("SET NAMES utf8");	
	$result = mysql_query("Select company,contact,costs,cpc,samplesize,url,complete,screenout,quotafull,extra1,extra2,key1,key2,mitsumori,id,projectname from ghostprojects where id=$id");	
	$record = mysql_fetch_array($result);
	}
	?>
	<form method="post" action="/redirect/TestNew/tasha.php">
			<table>
				<tr>
					<td>
						Company:<br>
						Drop-down box for asia only clients<br>
					</td>
					<td>
						Contact:<br>
						Drop-down box for contacs<br>
					</td>
					<td>
						Project Name:<br>
						<input type="text" name="projectname" value="<?=$record["projectname"]?>">
					</td>
				</tr>
				<tr>
					<td>
						CPC from company （原価）:<br>
						<input type="text" name="costs" value="<?=$record[2]?>"><br>
					</td>	
					<td>
						CPC to company （単価）:<br>
						<input type="text" name="cpc" value="<?=$record[3]?>"><br>
					</td>
					<td>
						Sample Size:<br>
						<input type="text" name="samplesize" value="<?=$record[4]?>"><br>
					</td>
				</tr>
				<tr>
					<td colspan="3">
						Access URL:<br>
						<input type="text" name="url" size="120" value="<?=$record[5]?>"><br>
					</td>
				</tr>
				<tr>
					<td colspan="3">	
						Complete:<br>
						<input type="text" name="complete" size="120" value="<?=$record[6]?>"><br>
					</td>
				</tr>
				<tr>
					<td colspan="3">
						Screenout:<br>
						<input type="text" name="screenout" size="120" value="<?=$record[7]?>"><br>
					</td>
				</tr>
				<tr>
					<td colspan="3">
						Quotafull:<br>
						<input type="text" name="quotafull" size="120" value="<?=$record[8]?>"><br>
					</td>
				</tr>
				<tr>
					<td colspan="3">
						Extra 1:<br>
						<input type="text" name="extra1" size="120" value="<?=$record[9]?>"><br>
					</td>
				</tr>
				<tr>
					<td colspan="3">
						Extra 2:<br>
						<input type="text" name="extra2" size="120" value="<?=$record[10]?>"><br>
					</td>
				</tr>
				<tr>
					<td>
						Key One:<br>
						<input type="text" name="key1" value="<?=$record[11]?>"><br>
					</td>
				</tr>
				<tr>
					<td>
						Key Two:<br>
						<input type="text" name="key2" value="<?=$record[12]?>"><br>
					</td>
					<td>	
						Mitsumori:<br>
						Dropdown box for <br>
					</td>
	
				</tr>
				<tr>
					<td id="mitsumori" colspan="3">
						Empty Space 
					</td>
				</tr>
				<tr>
				<td colspan="3">
					<?php
						if($id ==1)
						{
							?>
							<button type="button" class="navbutton" disabled="disabled">Previous:</button>
							<?php
						}
						else {
							?>
							<button type="button" class="navbutton" value=<?=$id-1?>>Previous:</button>
							<?php
						}
					?>
					<input type="submit" value="new" name="new-project">
					<input type="submit" value="submit" name="save-project">
					<input type="hidden" value="<?=$id?>" name="id">
					<?php
					$db = mysql_connect("mysql513.in.shared-server.net:16033", "JRtu434", "qvoiceDB1");
					mysql_select_db("JRtu434", $db);
					$result= mysql_query("Select max(ID) from ghostprojects");
					$count= mysql_fetch_array($result);
						if($id == $count[0])
						{
							?>
							<button type="button" class="navbutton" disabled="disabled">Next:</button>
							<?php
						}
						else {
							?>
							<button type="button" class="navbutton" value=<?=$id+1?>>Next:</button>
							<?php
						}
					?>
					<button type="button" id="id-toggle" value="<?=$record["id"]?>"> Show IDs</button>
				</td>
			</tr>
				</table>
				<input type="hidden" value="<?=$record["id"]?>">
			</form>
			
	<?php
	if ($id >=1)
	{
		echo "<hr>";
		echo "Access URL is: http://www.gain-www.com/redirect/import.php?gainid=$id&".$record["key1"]."=XXXXXXX&".$record["key2"]."=YYYYYYY<br>";
	}
	?>
	<hr>
	<div id="ids">
		
	</div>
	<?php
}

function  drawtable($state,$lang)
{
 $db = mysql_connect("mysql513.in.shared-server.net:16033", "JRtu434", "qvoiceDB1");
 mysql_select_db("JRtu434", $db);
 mysql_query("set SQL_BIG_Selects=1");
 $status = array("all"=>"All","live"=>"Live","closed"=>"Closed","temp"=>"Temp Closed","po"=>"Waiting for PO","ids"=>"Waiting for IDs","inv"=>"Waiting for Invoice","pay"=>"Waiting for Payment","test"=>"TEST","repo"=>"Report");
 echo	'<form name="F1">';
 echo	'<select name="users" onchange="reloadPHP(1,this.value)">';
 foreach ($status as $value=>$text)
		{
				if ($value == $state){
					echo '<option value="'.$value.'" selected="selected">'.$text.'</option>';
				}
				else {
					echo '<option value="'.$value.'">'.$text.'</option>';
				}
		}		
 echo	'</select>';
 echo	'<input type="checkbox" name="lang" value="ja" onclick="reloadPHP(1,document.F1.users[document.F1.users.selectedIndex].value)">日本語</input>';
 echo "</form>\n";
  
 if ($lang == "en")
 {
 	
	if($state == "all")
	{
	  	$result = mysql_query("Select projects.ID as ID,name as name,state as status,
	  	sum(if(status=\"complete\",1,0)) as complete,cpc,
	  	cpc*ss as mmkingaku,
  		cpc*sum(if(status=\"complete\",1,0)) as total,
	  	sum(if(status=\"screenout\",1,0)) as screenout,
	  	sum(if(status=\"quotafull\",1,0)) as quotafull, 
	  	sum(if(status=\"short\",1,0)) as short,
	  	sum(if(status=\"gate\",1,0)) as gate,
	  	sum(if(status is null,1,0)) as empty, 
	  	sum(if(status=\"complete\",1,0))/(sum(if(status=\"complete\",1,0))+sum(if(status=\"screenout\",1,0)))*100 as IR
	  	from testredirect,projects
	  	where testredirect.rid=projects.rid group by name order by complete desc");
	}
	else {
		$result = mysql_query("Select projects.ID as ID,name as name,state as status,
	  	sum(if(status=\"complete\",1,0)) as complete,cpc,
	  	cpc*ss as mmkingaku,
  		cpc*sum(if(status=\"complete\",1,0)) as total,
	  	sum(if(status=\"screenout\",1,0)) as screenout,
	  	sum(if(status=\"quotafull\",1,0)) as quotafull, 
	  	sum(if(status=\"short\",1,0)) as short,
		sum(if(status=\"gate\",1,0)) as gate,
		sum(if(status is null,1,0)) as empty, 
		sum(if(status=\"complete\",1,0))/(sum(if(status=\"complete\",1,0))+sum(if(status=\"screenout\",1,0)))*100 as IR
		from testredirect,projects
		where testredirect.rid=projects.rid and state=\"".$state."\" group by name order by complete desc");
	}
	
	echo "<form method=\"post\" action=\"/redirect/test/index2.php\">\n";
	echo "<table border='1' id='table'>\n";
	echo "<td>" . "IDs". "</td>\n";
	echo "<td>" . "Recent". "</td>\n";
	echo "<td>" . "Edit"."</td>\n";
	echo "<td>" . "Name". "</td>\n";
	echo "<td>" . "Contracted". "</td>\n";
	echo "<td>" . "Total". "</td>\n";
	echo "<td>" ."Complete". "</td>\n";
	echo "<td>" ."Screenout" . "</td>\n";
	echo "<td>" ."Quotafull" . "</td>\n";
	echo "<td>" ."Short" . "</td>\n";
	echo "<td>" ."Gate" . "</td>\n";
	echo "<td>" ."In Progress" . "</td>\n";
	echo "<td>" ."IR %" . "</td>\n";
	echo "<td>" . "Close". "</td>\n";
	echo "</tr>";
	$status = array("live"=>"Live","closed"=>"Closed","temp"=>"Temp Closed","po"=>"Waiting for PO","ids"=>"Waiting for IDs","inv"=>"Waiting for Invoice","pay"=>"Waiting for Payment","test"=>"TEST");
	setlocale(LC_MONETARY, 'en_US');
	$contractTotal =0;
	$runningTotal =0;
	$averageIR = 0;
	$count =0;
	while($row = mysql_fetch_array($result))
	  {
	  	if (($row['empty'])>($row['complete']+$row['screenout']))
			echo "<tr bgcolor=\"orange\"><td align=\"center\">";
		else if ($row['total'] > $row['mmkingaku'] && $row['mmkingaku']>0)
			echo "<tr bgcolor=\"green\"><td align=\"center\">";
		else
			echo "<tr><td align=\"center\">";
		echo "<button type=\"button\" name=\"download\" value=\"".$row['name'] ."\" onclick=\"reloadPHP(5,this.value)\">IDs</td>\n";
		echo "<td align=\"center\"><button type=\"button\" name=\"view\" value=\"".$row['ID']."\" onclick=\"reloadPHP(4,this.value)\">View</td>";
		echo "<td><button type=\"button\" name=\"view\" value=\"".$row['ID']."\" display=\"view\" onclick=\"reloadPHP(3,this.value)\">Edit</button></td>";
		echo "<td>" . $row['name'] . "</td>\n";
		echo "<td align=\"right\">" .money_format('%n',$row['mmkingaku']) . "</td>\n";
		echo "<td align=\"right\">" .money_format('%n',$row['total']) . "</td>\n";
		echo "<td align=\"right\">" . $row['complete'] . "</td>\n";
		echo "<td align=\"right\">" . $row['screenout'] . "</td>\n";
		echo "<td align=\"right\">" . $row['quotafull'] . "</td>\n";
		echo "<td align=\"right\">" . $row['short'] . "</td>\n";
		echo "<td align=\"right\">" . $row['gate'] . "</td>\n";
		echo "<td align=\"right\">" . $row['empty'] . "</td>\n";
		echo "<td align=\"right\">" . number_format($row['IR'],2) . "%</td>\n";
		echo "<td><select onchange=\"reloadPHP(2,this.value)\">\n";
		foreach ($status as $value=>$text)
		{
				if ($value == $row['status']){
					echo "<option value=\"".$row['ID']."&state=".$value."\" selected=\"selected\">$text</option>\n";
				}
				else {
					echo "<option value=\"".$row['ID']."&state=".$value."\">$text</option>\n";
				}
		}

		echo "</select></td>\n";
		echo "</tr>\n";
		$contractTotal += $row['mmkingaku'];
		$runningTotal += $row['total'];
		$count++;
	  }
	echo "<tr>\n";
	echo "<b><td colspan=2>Total Contracted:</td>\n";
	echo "<b><td>".money_format('%n',$contractTotal)."</td>\n";
	echo "<b><td colspan=2>Total Running:</td>\n";
	echo "<b><td>".money_format('%n',$runningTotal)."</td>\n";
	echo "<b><td colspan=2>Average Running:</td>\n";
	echo "<b><td>".money_format('%n',($runningTotal/$count))."</td>\n";
	echo "</tr>\n";
  	echo "</table>\n";
  
	echo "<input type=\"submit\" value=\"submit\" name=\"submit\">\n";
	echo "</form>\n";
	echo "Current server time: ".time();
}
else {
	
	mysql_query("SET NAMES utf8");	
	mysql_query("set SQL_BIG_Selects=1");
	if($state == "all")
	{
	  	$result = mysql_query("Select projects.ID as ID,jname as name, cid,state as status,
	  	sum(if(status is not null,1,0)) as respondents,
	  	sum(if(status is null,1,0)) as empty, 
	  	sum(if(status=\"complete\",1,0))/(sum(if(status=\"complete\",1,0))+sum(if(status=\"screenout\",1,0)))/.25 as invites
	  	from testredirect,projects
	  	where testredirect.rid=projects.rid group by name");
	}
	else {
		$result = mysql_query("Select projects.ID as ID,jname as name, cid,state as status,
	  	sum(if(status is not null,1,0)) as respondents,
	  	sum(if(status is null,1,0)) as empty, 
	  	sum(if(status=\"complete\",1,0))/(sum(if(status=\"complete\",1,0))+sum(if(status=\"screenout\",1,0)))/.25 as invites
	  	from testredirect,projects
		where testredirect.rid=projects.rid and state=\"".$state."\" group by name desc");
	}
	
	echo "<form method=\"post\" action=\"/redirect/test/index2.php\">\n";
	echo "<table border='1'>\n";
	echo "<td>" . "IDs". "</td>\n";
	echo "<td>" . "最近". "</td>\n";
	echo "<td>" . "CID". "</td>\n";
	echo "<td>" . "調査名". "</td>\n";
	echo "<td>" . "回収数". "</td>\n";
	echo "<td>" ."途中まで". "</td>\n";
	echo "<td>" ."配信数（適当に）" . "</td>\n";
	echo "<td>" ."ステータス" . "</td>\n";
	echo "</tr>";
	$status = array("live"=>"ライブ","closed"=>"閉じてる","temp"=>"一時閉じた","po"=>"PO待ち","ids"=>"ＩＤ待ち","inv"=>"請求書待ち","pay"=>"未入金","test"=>"テスト中");
	
	while($row = mysql_fetch_array($result))
	  {
		echo "<tr><td><input type=\"radio\" name=\"download\" value=\"".$row['name'] ."\"></td>\n";
		echo "<td><input type=\"button\" name=\"view\" value=\"".$row['ID']."\" display=\"view\" onclick=\"reloadPHP(4,this.value)\"></td>";
		echo "<td>" . $row['cid'] . "</td>\n";
		echo "<td>" . $row['name'] . "</td>\n";
		echo "<td>" . $row['respondents'] . "</td>\n";
		echo "<td>" . $row['empty'] . "</td>\n";
		echo "<td>" . $row['invites'] . "</td>\n";
		echo "<td>".$status[$row['status']]."</td>\n";
		echo "</tr>\n";
		
	  }
  	echo "</table>\n";
  
	echo "<input type=\"submit\" value=\"submit\" name=\"submit\">\n";
	echo "</form>\n";
}
mysql_close();
}

function drawbrowser($id)
{
	echo "<form method=\"post\" action=\"/redirect/TestNew/editproject.php\">";
	$status = array("live"=>"Live","closed"=>"Closed","temp"=>"Temp Closed","po"=>"Waiting for PO","ids"=>"Waiting for IDs","inv"=>"Waiting for Invoice","pay"=>"Waiting for Payment","test"=>"TEST");
	$db = mysql_connect("mysql513.in.shared-server.net:16033", "JRtu434", "qvoiceDB1");
	mysql_select_db("JRtu434", $db);
	mysql_query("set SQL_BIG_Selects=1");
	mysql_query("SET NAMES utf8");
	$result = mysql_query("Select max(ID) from projects");
	$data = mysql_fetch_array($result);
	$max = $data[0];	
	// show the browse option if ID > 0 or ID = -1
	if($id!=0)
	{
		if ($id ==-1)
		{	
			//set the browser to the most recent project
			$id = $max;
		}
		$result = mysql_query("Select ID,name from projects order by ID DESC");
		echo "<select onchange=\"showproject(this.value)\">\n";
		while($record = mysql_fetch_array($result))
			{
				if ($id == $record[0])
				echo "<option value=\"".$record[0]."\" selected=\"selected\">".$record[1]."</option>\n";
				else {
				echo "<option value=\"".$record[0]."\">".$record[1]."</option>\n";
				}				
			}
		echo "</select>\n";
		$result = mysql_query("Select max(ID) from projects");
		$count = $id;
		if ($data = mysql_fetch_array($result))
		$count = $data[0];
		$result = mysql_query("Select ID from projects order by ID ASC");
		while ($data = mysql_fetch_array($result))
		{
			if ($id <= $data[0])
			{
				$id = $data[0];
				break;
			}
		}
		$result = mysql_query("Select * from projects where ID=\"$id\" ");
		if ($data = mysql_fetch_array($result))
		{
			echo "<table>";
			echo "<tr>";
			echo "<td>";
			echo "RID（暗号）: <br><input type=\"text\" size=\"30\" maxlength=\"30\" name=\"rid\" value=\"$data[0]\">";
			echo "</td>";
			echo "<td>";
			echo "Study Name: <br><input type=\"text\" size=\"30\" maxlength=\"60\" name=\"name\" value=\"$data[1]\">";
			echo "</td>";
			echo "<td>";
			echo "State： <br><select name=\"state\">\n";
			foreach ($status as $value=>$text)
			{
				if ($value == $data[3]){
					echo "<option value=\"".$value."\" selected=\"selected\">$text</option>\n";
				}
				else {
					echo "<option value=\"".$value."\">$text</option>\n";
				}
			}

			echo "</select></td>\n";
			echo "</td><td>Registered:";
			if($data["rashinban"]==1)
					  echo '<input type="checkbox" name="rashinban" checked="yes" value="1">';
					else {
					  echo '<input type="checkbox" name="rashinban" value="1">';
					}
			echo "</td></tr>";
			echo "<tr>";
			echo "<td>";
			echo "CID: <br><input type=\"text\" size=\"30\" maxlength=\"60\" name=\"cid\" value=\"$data[12]\">";
			echo "</td>";
			echo "<td>";
			echo "Study Name (日本語)：<br><input type=\"text\" size=\"30\" maxlength=\"60\" name=\"jname\" value=\"$data[10]\">";
			echo "</td>";
			echo "<td>";
			echo "URL：<br><input type=\"text\" size=\"30\" maxlength=\"200\" name=\"url\" value=\"$data[11]\">";
			echo "</td>";
			echo "</tr>";
			echo "<tr>";
			echo "<td>";
			echo "Invites: <br><input type=\"number\" size=\"30\" maxlength=\"60\" name=\"invites\" value=\"$data[4]\">";
			echo "</td>";
			echo "<td>";
			echo "CPC：<br><input type=\"text\" size=\"30\" maxlength=\"60\" name=\"cpc\" value=\"$data[5]\">";
			echo "</td>";
			echo "<td>";
			echo "SS：<br><input type=\"number\" size=\"30\" maxlength=\"60\" name=\"ss\" value=\"$data[6]\">";
			echo "</td>";
			echo "</tr>";
			echo "<td>";
			echo "Forex : <br><input type=\"text\" size=\"30\" maxlength=\"60\" name=\"forex\" value=\"$data[7]\">";
			echo "</td>";
			echo "<td>";
			echo "Questions：<br><input type=\"number\" size=\"30\" maxlength=\"60\" name=\"questions\" value=\"$data[8]\">";
			echo "</td>";
			echo "<td>";
			echo "Mitsumori ID：<br>";
			echo  drawmitsumoridropbox($data[9]);
			echo "</td>";
			echo "</tr>";
			echo "<td>";
			$prev = $id-1;
			if($id > 1)
				echo "<input type=\"button\" value=\"Prev\" onClick=\"showproject($prev)\">";
			else {
				echo "<input type=\"button\" value=\"Prev\" disabled=\"disabled\" onClick=\"showproject($prev)\">";
			}
			echo "</td>";
			echo "<td>";
			echo "<input type=\"submit\" value=\"submit\" name=\"submit\">";
			echo "</td>";
			echo "<td>";
			$next = $id+1;			
			if($id < $count)
				echo "<input type=\"button\" value=\"next\" onClick=\"showproject($next)\">";
			else {
				echo "<input type=\"button\" value=\"next\" disabled=\"disabled\" onClick=\"showproject($next)\">";
			}
			echo "</td>";
			echo "</tr>";
			echo "</table>";
			echo "<input type=\"hidden\" name=\"id\" value=\"$id\">";
			echo "</form>";
		}
		else {
			$id--;
			drawbrowser($id);
		}
		mysql_close();
	}
	else {
		echo "<table>";
			echo "<tr>";
			echo "<td>";
			echo "RID（暗号）: <br><input type=\"text\" size=\"30\" maxlength=\"30\" name=\"rid\" >";
			echo "</td>";
			echo "<td>";
			echo "Study Name: <br><input type=\"text\" size=\"30\" maxlength=\"60\" name=\"name\" >";
			echo "</td>";
			echo "<td>";
			echo "State： <br><select name=\"state\">\n";
			foreach ($status as $value=>$text)
			{
				if ($value == $data[3]){
					echo "<option value=\"".$value."\" selected=\"selected\">$text</option>\n";
				}
				else {
					echo "<option value=\"".$value."\">$text</option>\n";
				}
			}
			echo "</select></td>\n";
			echo "</td>";
			echo "</tr>";
			echo "<tr>";
			echo "<td>";
			echo "CID: <br><input type=\"text\" size=\"30\" maxlength=\"60\" name=\"cid\" value=\"\">";
			echo "</td>";
			echo "<td>";
			echo "Study Name (日本語)：<br><input type=\"text\" size=\"30\" maxlength=\"60\" name=\"jname\" value=\"\">";
			echo "</td>";
			echo "<td>";
			echo "URL：<br><input type=\"text\" size=\"30\" maxlength=\"200\" name=\"url\" value=\"\">";
			echo "</td>";
			echo "</tr>";
			echo "<tr>";
			echo "<td>";
			echo "Invites: <br><input type=\"number\" size=\"30\" maxlength=\"60\" name=\"invites\" >";
			echo "</td>";
			echo "<td>";
			echo "CPC：<br><input type=\"text\" size=\"30\" maxlength=\"60\" name=\"cpc\" >";
			echo "</td>";
			echo "<td>";
			echo "SS：<br><input type=\"number\" size=\"30\" maxlength=\"60\" name=\"ss\" >";
			echo "</td>";
			echo "</tr>";
			echo "<tr>";
			echo "<td>";
			echo "Forex <br><input type=\"text\" size=\"30\" maxlength=\"60\" name=\"forex\" >";
			echo "</td>";
			echo "<td>";
			echo "Questions：<br><input type=\"number\" size=\"30\" maxlength=\"60\" name=\"questions\" >";
			echo "</td>";
			echo "<td>";
			echo "Mitsumori ID：<br>";
			echo  drawmitsumoridropbox(0);
			echo "</td>";
			echo "</tr>";
			echo "<tr>";
			echo "<td>";
			echo "</td>";
			echo "<td>";
			echo "<input type=\"submit\" value=\"submit\" name=\"submit\">";
			echo "</td>";
			echo "<td>";
			echo "</td>";
			echo "</tr>";
			echo "</table>";
			echo "<input type=\"hidden\" name=\"id\" value=\"$id\">";
			echo "</form>";
	}
	
}

function drawlastIDs($id)
{
	
	$db = mysql_connect("mysql513.in.shared-server.net:16033", "JRtu434", "qvoiceDB1");
	mysql_select_db("JRtu434", $db);
	mysql_query("set SQL_BIG_Selects=1");
	 echo "<p>Last IDs</p>";
	 echo "<table border='1'><tr>";
	 echo "<td>Num</td><td>ID</td><td>Project</td><td>Status</td><td>Start</td><td>End</td><td>Time</td></tr>";
	$i=0;
	$db = mysql_connect("mysql513.in.shared-server.net:16033", "JRtu434", "qvoiceDB1");
	mysql_select_db("JRtu434", $db);
	$res = mysql_query("Select testredirect.ID,name,status,end,start from testredirect,projects where testredirect.rid=projects.rid and projects.ID=\"$id\" order by end DESC ");

	while($row = mysql_fetch_array($res))
	{
	$i = $i+1;

	$duration = ($row['end']-$row['start']);
	echo "<tr><td>".$i."</td><td>" . $row['ID'] . "</td><td>" . $row['name'] . "</td><td>" . $row['status'] . "</td><td>" .strftime('%m/%d %X',$row['start']) . "</td><td>" . strftime('%m/%d %X',$row['end']) . "</td><td>" . strftime('%M:%S',$duration) ."</td></tr>";
	}
	echo "</table>";
	mysql_close();
}
function companybrowser($id)
{
	$currentRecord;
	$count;
?>
	<form action="company.php" method="post">
		<?php
		if ($id>0)
		{
			?>
		<table>
			<tr>
				<td colspan="3" align="right">
					<?php
					//show the company list if in browse mode
					$db = mysql_connect("mysql513.in.shared-server.net:16033", "JRtu434", "qvoiceDB1");
					mysql_select_db("JRtu434", $db);
					$res = mysql_query("select count(ID) from companies");
					$record = mysql_fetch_array($res);
					$count = $record[0];
					echo $id."/".$count;
					$result = mysql_query("select * from companies order by ID ASC");
					echo '</td></tr><tr><td>';
					echo "Company： <br><select name=\"company\" id=\"companyselect\">\n";
					while($record = mysql_fetch_array($result))
					{
						if ($id == $record["ID"]){
						$currentRecord = $record;
						echo "<option value=\"".$currentRecord["ID"]."\" selected=\"selected\">".$currentRecord['Name']."</option>\n";
						
						}
						else {
						echo "<option value=\"".$record["ID"]."\">".$record['Name']."</option>\n";
						}
					}
					echo "</select>";
					?>
				</td>
				<td>
					Asian Alliance:
					<?php
					if($currentRecord["asianalliance"]==1)
					  echo '<input type="checkbox" name="asianalliance" checked="yes" value="1">';
					else {
					  echo '<input type="checkbox" name="asianalliance" value="1">';
					}
					?>
				</td>
				<td rowspan="6">
					<button type="button" name="download" value="client" onclick="reloadPHP(this.value,0)">Client Mailing List</button>;
					<br>
					Associated Contacts:<br>  
					<?php
					$res2 = mysql_query("select count(NAME) from contacts where COMPID=$id");
					$record = mysql_fetch_array($res2);
					echo "<select multiple size='20'>";
					if($record[0]==0)
					echo "<OPTION>No matches found</option>";
					else {					
					$res2 = mysql_query("select NAME,ID from contacts where COMPID=$id");
					while($record = mysql_fetch_array($res2))
						echo "<OPTION VALUE=\"".$record[1]."\">$record[0]</option>";
					}
					?>
					</select><br>
				<button type="button" id="addpane">Add new Contact</button><br>
				<div id="slideout" style="display:none;">
						Name:<br>
						<input type="text" name="contname"/><br>
						Email:<br>
						<input type="text" name="email"/><br>
						Phone:<br>
						<input type="text" name="contphone"/><br>
						Country:<br>
						<input type="text" name="contcountry"/><br>
						<input type="submit" name="add-contact" value="Add"><br>
				</div>
				<button type="button">Edit Selected</button><br>
				<button type="button">Set as Main</button><br>
				</td>
			<tr>
				<td>
					<table>
						<tr>
							<td>Company: <br>
							<?php
							echo "<input type=\"text\" size=\"30\" maxlength=\"60\" name=\"name\" value=\"".$currentRecord['Name']."\">";
							?>
							</td>
							<td>
								Short Name: <br>
								<input type="text" size="10" maxlength="25" name="shortname" value="<?=$currentRecord["shortname"]?>">
							</td>
						</tr>
					</table>
				</td>
				<td align ="right">					
					Status:<br>
					<select name="status">
					<?php
					$Status =  array("client"=> "Client","pot"=>"Potential","met"=>"Met/Talked With","contacted"=>"Mailed/Called","named"=>"List up only","ref"=>"Refused");
					foreach ($Status as $value => $text) {
						if($currentRecord["Status"] == $value)
							echo '<option value="'.$value.'" selected="selected">'.$text.'</option>';
						else
							echo '<option value="'.$value.'">'.$text.'</option>';
					}
					?>
					
									
					</select>
				</td>
			</tr>
			<tr>
				<td>
					Primary Contact:<br>
					<select name="contact">
					<?php
					$res2 = mysql_query("select count(NAME) from contacts where COMPID=$id");
					$record = mysql_fetch_array($res2);
					if($record[0]==0)
					 echo "<OPTION>No matches found</option>";
					else {
					$res2 = mysql_query("select NAME,ID from contacts where COMPID=$id");
					while($record = mysql_fetch_array($res2))
						if($currentRecord["Contact"]==$record[1])
						echo "<OPTION VALUE=\"".$record[1]."\" selected=\"Selected\">$record[0]</option>";
						else {
							echo "<OPTION VALUE=\"".$record[1]."\">$record[0]</option>";
						}
					}
					?>
					</select>
				</td>
				<td align ="right">
					Rank:<br>
					<select name="rank">
						<?php
						$ranks =  array("1"=> "A","2"=>"B","3"=>"C","4"=>"D","5"=>"E","6"=>"F");
						foreach ($ranks as $value => $text) {
							if($currentRecord["rank"] == $value)
								echo '<option value="'.$value.'" selected="selected">'.$text.'</option>';
							else
								echo '<option value="'.$value.'">'.$text.'</option>';
						}
						?>
					</select>
				</td>
			</tr>
			<tr>
				<td>
					Country: <br>
					<input type="text" size= "30" maxlength= "80" name="country" value="<?=$currentRecord['country']?>"><br>
					Address: <br>
					<input type="text" size= "30" maxlength= "80" name="address" value="<?=$currentRecord['Address']?>">
				</td>
				<td align="right">
					System:
					<?php
					if($currentRecord["System"]==1)
					  echo '<input type="checkbox" name="system" checked="yes" value="1">';
					else {
					  echo '<input type="checkbox" name="system" value="1">';
					}
					?>
					<br>
					Knowhow:
					<?php
					if($currentRecord["knowhow"]==1)
					  echo '<input type="checkbox" name="knowhow" checked="yes" value="1">';
					else {
					  echo '<input type="checkbox" name="knowhow" value="1">';
					}
					?>
					<br>
					Panel: 
					<?php
					if($currentRecord["panel"]==1) {
						echo '<input type="checkbox" name="panel" checked="yes" value="1" id="panel"><br>';
						echo '<input type="number" name="panelsize" id="panelbox" value="'.$currentRecord['panelsize'].'">';
					}
					else {
					  echo '<input type="checkbox" name="panel" value="1" id="panel"><br>';
						echo '<input type="number" name="panelsize" id="panelbox" value="'.$currentRecord['panelsize'].'" style="display: none;">';
					}
					?>
					<br>
					
				</td>
			</tr>
			<tr>
				<td>
					Website: <br>
					<input type="text" size= "30" maxlength= "60" name="web" value="<?=$currentRecord['web']?>">
				</td>
				<td align="right">
					Company Size:<br>
					<select name="size">
					<?php
					$size =  array("0"=> "Very Small","3"=>"Small","5"=>"Medium","7"=>"Large","10"=>"Extra Large");
					foreach ($size as $value => $text) {
						if($currentRecord["size"] == $value)
							echo '<option value="'.$value.'" selected="selected">'.$text.'</option>';
						else
							echo '<option value="'.$value.'">'.$text.'</option>';
					}
					?>	
					</select>
				</td>
			</tr>
			<tr>
				<td>
					Notes: <br>
					<textarea rows="20" cols="50"  name="notes"><?=$currentRecord['notes']?></textarea>
				</td>
				<td>
					Organizations<br>
					Casro:
					<?php
					if($currentRecord["Casro"]==1)
					  echo '<input type="checkbox" name="casro" checked="yes" value="1">';
					else {
					  echo '<input type="checkbox" name="casro" value="1">';
					}
					?><br>
					Esomar:
					<?php
					if($currentRecord["Esomar"]==1)
					  echo '<input type="checkbox" name="esomar" checked="yes" value="1">';
					else {
					  echo '<input type="checkbox" name="esomar" value="1">';
					}
					?><br>
				</td>
			</tr>
			<tr>
				<td>
					<?php
					if($id==1)
						echo '<button type="button" onclick="" disabled="disabled">Prev</button>';
					else 
						echo '<button type="button" id="prev" value="'.($id-1).'">Prev</button>'					
					?>
				</td>
				<td align="center">
				<button type="button" id="new" value="new">New</button>
				<input type="hidden" value=<?=$id?> name="id">
				<input type="submit" value="submit" name="update-comp">
				</td>
				<td>
					<?php
					if(($id+1)>$count)
						echo '<button type="button" onclick="" disabled="disabled">Next</button>';
					else 
						echo '<button type="button" id="next" value="'.($id+1).'">Next</button>'					
					?>
				</td>
			</tr>
			<tr>
				<td>
					Last Updated: <?=strftime('%m/%d %X',$currentRecord["moddate"])?>
				</td>
			</tr>
		</table>	
		
	</form>
		
<?php
mysql_close;
}
else {
	?>
		<table>
			<tr><td align="center" ><br>Enter a new Company<br></td></tr>
			<tr>
				<td>
					<button type="button" onClick="reloadPHP(6,2)">Switch to browse mode</button>
				</td>
			</tr>
		</table>	
	</form>
			
	<?php
	}
}
	

function drawmitsumori($id)
{
	$feasibility = array("feasible"=>"回収可能","refused"=>"断り","1"=>"1.5倍","2"=>"2倍","3"=>"3倍","4"=>"4倍","5"=>"5倍以上");
	$status = array("p"=>"Pending","r"=>"Refused","w"=>"Won","f"=>"Needs Follow-up");
	$db = mysql_connect("mysql513.in.shared-server.net:16033", "JRtu434", "qvoiceDB1");
 	mysql_select_db("JRtu434", $db);
	mysql_query("Set Names utf8");
	if ($id==0)
	{
	$res = mysql_query("Select max(ID) from RFQS");
	$res2 = mysql_fetch_array($res);
	$id = $res2[0];	
	}
	$res = mysql_query("Select * from RFQS where ID=$id");
		
	$record = mysql_fetch_array($res);
	?>
	<form name="frm" method="post" action="/redirect/TestNew/mitsumori.php">
		<div id="slideout" style="display:none;z-index: 100;position: absolute; border: 1; left: 620; top: 20; float: right; background-color: white;">
					&nbsp;&nbsp;Name:<br>
					&nbsp;&nbsp;<input type="text" name="contname"/>&nbsp;&nbsp;<br>
					&nbsp;&nbsp;Email:<br>
					&nbsp;&nbsp;<input type="text" name="email"/>&nbsp;&nbsp;<br>
					&nbsp;&nbsp;Phone:<br>
					&nbsp;&nbsp;<input type="text" name="contphone"/>&nbsp;&nbsp;<br>
					&nbsp;&nbsp;Country:<br>
					&nbsp;&nbsp;<input type="text" name="contcountry"/>&nbsp;&nbsp;<br>
					&nbsp;&nbsp;<button type="button" id="add-contact" value="11">Add&nbsp;&nbsp;<br>
			</div>
		<table border="1">
			<tr>
				<td>Date: <br>
					<?php 
					if ($record["odate"]>0)
					{
					 ?><input class="datepicker" type="text" name="odate" value="<?=strftime('%Y/%m/%d',$record["odate"])?>"></input><?php	
					}
					else {
					 ?><input class="datepicker" type="text" name="odate" value="<?=strftime('%Y/%m/%d',time())?>"></input><?php		
					}?>
				</td>
				<td colspan="2">
					<?php echo drawmitsumoridropbox($id);?>
				</td>
				<td >
				<button type="button" class="navbutton" value="new">New</button>
				</td>
				<td>Status:<br>
					<select name="status">
					<?php
						foreach ($status as $value => $text) {
							if ($record["status"] == $value)
							echo '<option value="'.$value.'" selected="selected">'.$text.'</option>';
							else {
								echo '<option value="'.$value.'">'.$text.'</option>';
							}	
						}
					?>
					</select>
				</td>
				<td>
					Browsing: <?php 
					$count = mysql_query("select max(id) from RFQS");
					$max = mysql_fetch_array($count);
					echo '<select name="idbrowse" class="idbrowser">';
					for ($i=$max[0]; $i >0; $i--) {
						if ($id == $i)
							echo '<option id="'.$i.'" selected="selected">'.$i.'</option>'; 
						else {
							echo '<option id="'.$i.'">'.$i.'</option>';
						}
					}
					echo '</select>';
				?>
				</td>
			</tr>
			<tr>
				<td>
					Mail Subject:<br>
					<input name="subject" type="text" value="<?=$record["subject"]?>"></input>
				</td>
				<td>
					Sample Size:<br>
					<input name="samplesize" type="number" value="<?=$record["samplesize"]?>">
				</td>
				<td>
					出現率:<br>
					<input name="IR" type="text" value="<?=$record["IR"]?>">
					
				</td>
				<td>
					LOI:<br>
					<input name="LOI" type="text" value="<?=$record["LOI"]?>">
				</td>
				<td>
					B2B<br>
					<?php
						if($record["B2B"]==1)
							echo '<input name="B2B" type="checkbox" checked="yes" value="1">';
						else 
							echo '<input name="B2B" type="checkbox" value="1">';
					?>
					
				</td>
				<td>
					回収可能判断：<br>
					<select name="feasibility">
						<?php
						foreach ($feasibility as $value => $text) {
							if($record["feasibility"] == $value)
								echo '<option value="'.$value.'" selected="selected">'.$text.'</option>';
							else
								echo '<option value="'.$value.'">'.$text.'</option>';
							}		
						
						?>
					</select>
				</td>
			</tr>
			<tr>
				<td colspan="2">
				クライアント名：<br>
				<select id="companyselector" name="company">
					<?php
					$db = mysql_connect("mysql513.in.shared-server.net:16033", "JRtu434", "qvoiceDB1");
					mysql_select_db("JRtu434", $db);
					$res = mysql_query("Select * from companies order by ID ASC");
					while ($comp = mysql_fetch_array($res))
					{
						if($record["company"] == $comp["ID"])
								echo '<option value="'.$comp["ID"].'" selected="selected">'.$comp["Name"].'</option>';
						else
							echo '<option value="'.$comp["ID"].'">'.$comp["Name"].'</option>';
					}
				?>
				</select>
				</td>
				<td>
					Contact:<br>
					
					<select id="contacts" name="contact">
					<?php
					$db = mysql_connect("mysql513.in.shared-server.net:16033", "JRtu434", "qvoiceDB1");
					mysql_select_db("JRtu434", $db);
					if($record["company"]<1)
					$result= mysql_query("Select * from contacts where COMPID=\"1\" order by name asc");
					else
					$result= mysql_query("Select * from contacts where COMPID=\"".$record["company"]."\" order by name asc");
					while ($comp = mysql_fetch_array($result))
					{
						if($record["contact"] == $comp["ID"])
							echo '<option value="'.$comp["ID"].'" selected="selected">'.$comp["NAME"].'</option>';
						else
							echo '<option value="'.$comp["ID"].'">'.$comp["NAME"].'</option>';
								
					}
					?>
					</select>
					<button type="button" id="addpane">+1</button>
					
				
				</td>
				<td>
					単価：<br>
					<input type="text" name="cpc" value="<?=$record["cpc"]?>" placeholder="US$">
				</td>
				<td colspan="2">
					Genre:<br>
					<select name="genre">
						<?php
					$db = mysql_connect("mysql513.in.shared-server.net:16033", "JRtu434", "qvoiceDB1");
					mysql_select_db("JRtu434", $db);
					$result= mysql_query("Select * from genre order by name asc");
					while ($genre = mysql_fetch_array($result))
					{
						if($record["genre"] == $genre["ID"])
							echo '<option value="'.$genre["ID"].'" selected="selected">'.$genre["NAME"].'</option>';
						else
							echo '<option value="'.$genre["ID"].'">'.$genre["NAME"].'</option>';
							
					}
					mysql_close();
					?>
					</select>
				</td>
			</tr>
			<tr>
				<td colspan="3">
					RFQ Subject:<br>
					<textarea cols="80" rows="30" name="request"><?=$record["request"]?></textarea>
				</td>
				<td colspan="3">
					RFQ reply:<br>
					<textarea cols="80" rows="30" name="reply"><?=$record["reply"]?></textarea>
				</td>
			</tr>
			<tr>
				<td colspan="6">
					<?php
						if($id ==1)
						{
							?>
							<button type="button" class="navbutton" disabled="disabled">Previous:</button>
							<?php
						}
						else {
							?>
							<button type="button" class="navbutton" value=<?=$id-1?>>Previous:</button>
							<?php
						}
					?>
					
					<input type="submit" value="submit" name="save-mitsumori">
					<input type="hidden" value="<?=$id?>" name="id">
					<?php
					$db = mysql_connect("mysql513.in.shared-server.net:16033", "JRtu434", "qvoiceDB1");
					mysql_select_db("JRtu434", $db);
					$result= mysql_query("Select max(ID) from RFQS");
					$count= mysql_fetch_array($result);
						if($id == $count[0])
						{
							?>
							<button type="button" class="navbutton" disabled="disabled">Next:</button>
							<?php
						}
						else {
							?>
							<button type="button" class="navbutton" value=<?=$id+1?>>Next:</button>
							<?php
						}
					?>
				</td>
			</tr>
			<tr>
				<td colspan="6">
					<button>Follow up:</button><br>
					<input type="text" name="followup" class="datepicker">
				</td>
			</tr>
		</table>
	</form>
	<?php
}

function drawcontacts($id)
{
	$db = mysql_connect("mysql513.in.shared-server.net:16033", "JRtu434", "qvoiceDB1");
 	mysql_select_db("JRtu434", $db);
	mysql_query("Set Names UTF8");
	$result = mysql_query("select ID,NAME from contacts where COMPID=$id order by ID desc");
	while ($record = mysql_fetch_array($result))
	{
		echo '<option value="'.$record["ID"].'">'.$record["NAME"].'</option>';
	}		
}

if($_GET["func"] == 1)
{
	if ($_GET["state"] == "repo")
		drawreport();
	else
	    drawtable($_GET["state"],$_GET["lang"]);
}
else if($_GET["func"] == 2)
{
	$id = $_GET["id"];
	$state = $_GET["state"];
	$db = mysql_connect("mysql513.in.shared-server.net:16033", "JRtu434", "qvoiceDB1");
 	mysql_select_db("JRtu434", $db);
 	$result = mysql_query("select id from projects where name in (select name from projects where id=\"$id\")");
 	while ( $multID = mysql_fetch_array($result))
 	{
 		mysql_query("Update projects set state=\"$state\" where id=\"$multID[0]\"");
 	}
	mysql_close();
	drawtable($_GET["status"],$_GET["lang"]);
}

else if($_GET["func"] == 3)

{
	$id = $_GET["id"];
	drawbrowser($id);
} 

else if($_GET["func"] == 4)
{
	$id = $_GET["id"];
	drawlastIDs($id);
} 
else if($_GET["func"] == 5)
{
	$id = $_GET["id"];
	exportcsv($id);
} 
else if($_GET["func"] == 6)
{
	if ($_GET["id"] == "new")
	{
		$db = mysql_connect("mysql513.in.shared-server.net:16033", "JRtu434", "qvoiceDB1");
		mysql_select_db("JRtu434", $db);
		mysql_query("Insert into companies () values()");
		$record = mysql_query("select max(ID) from companies");
		$result = mysql_fetch_array($record);
		$id = $result[0];
		companyBrowser($id);
	}
	else {
	companyBrowser($_GET["id"]);
	}
}

else if($_GET["func"] == 7)
{
	$db = mysql_connect("mysql513.in.shared-server.net:16033", "JRtu434", "qvoiceDB1");
 	mysql_select_db("JRtu434", $db);
	
	$res2 = mysql_query("Select Contact from companies where id =\"".$_GET["id"]."\"");
	$con1 = mysql_fetch_array($res2);
	$result= mysql_query("Select * from contacts where COMPID=\"".$_GET["id"]."\"");
	while ($contact = mysql_fetch_array($result))
	{
		if($con1[0]=$contact["ID"])
			echo '<option value="'.$contact["ID"].'" selected="selected">'.$contact["NAME"].'</option>';
		else {
			echo '<option value="'.$contact["ID"].'">'.$contact["NAME"].'</option>';
		}
	}
}

else if($_GET["func"] == 8)
{
	
	if ($_GET["id"] == "new")
	{
		$db = mysql_connect("mysql513.in.shared-server.net:16033", "JRtu434", "qvoiceDB1");
		mysql_select_db("JRtu434", $db);
		mysql_query("Insert into RFQS () values()");
		$record = mysql_query("select max(ID) from RFQS");
		$result = mysql_fetch_array($record);
		$id = $result[0];
		drawmitsumori($id);
	}
	else 
	{
		drawmitsumori($_GET["id"]);
	}

}
else if($_GET["func"] == 9)
{
		ghostproject($_GET["id"]);
}
else if($_GET["func"] == "client")
{
	exportmailinglist();
}
else if($_GET["func"] == 10)
{
		$db = mysql_connect("mysql513.in.shared-server.net:16033", "JRtu434", "qvoiceDB1");
		mysql_select_db("JRtu434", $db);
		$result = mysql_query("Select * from ghostmonitors where gainid=".$_GET["id"]);
		$company = mysql_fetch_array(mysql_query("select key1,key2 from ghostprojects where id=".$_GET["id"]));
		?>
		<table>
			<tr>
				<th>
					ID
				</th>
				<th>
					<?=$company["key1"]?>
				</th>
				<th>
					<?=$company["key2"]?>
				</th>
				<th>
					Start Time
				</th>
				<th>
					End Time
				</th>
				<th>
					Status
				</th>
			</tr>
			<?php
			while ($record = mysql_fetch_array($result))
			{
			?>		
			<tr>
				<td>
					<?=$record["id"]?>
				</td>
				<td>
					<?=$record["key1value"]?>
				</td>
				<td>
					<?=$record["key2value"]?>
				</td>
				<td>
					<?=strftime("%m/%d %X",$record["starttime"])?>
				</td>
				<td>
					<?=strftime("%m/%d %X",$record["endtime"])?>
				</td>
				<td>
					<?=$record["status"]?>
				</td>
			</tr>
			<?php
			}
			?>
		</table>
		<?php
		
}
else if($_GET["func"] == 11)
{
	$db = mysql_connect("mysql513.in.shared-server.net:16033", "JRtu434", "qvoiceDB1");
	mysql_select_db("JRtu434", $db);
	mysql_query("set names utf8");
	$name = $_GET["contname"];
	$compid = $_GET["company"];
	$email = $_GET["email"];
	$country = $_GET["country"];
	$phone = $_GET["phone"];
	if(strlen($name)>0)
		mysql_query("insert into contacts (NAME,COMPID,EMAIL,Country,phone) values (\"".$name."\",\"".$compid."\",\"".$email."\",\"".$country."\",\"".$phone."\")");
	$result= mysql_query("Select * from contacts where COMPID=$compid");
	while ($contact = mysql_fetch_array($result))
	{
		echo '<option value="'.$contact["ID"].'">'.$contact["NAME"].'</option>';
	}
}

?>
